<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>truncate | share-open</title>
    <meta name="description" content="函数库，工具库">
    <link rel="stylesheet" href="/share-open/assets/style.25a24194.css">
    <link rel="modulepreload" href="/share-open/assets/app.a7d6c65a.js">
    <link rel="modulepreload" href="/share-open/assets/StringUtils_truncate.md.debe512b.lean.js">
    
    <link rel="icon" href="/share-open/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  <meta name="keywords" content="@minionorg,share,函数库,工具库">
  <meta name="twitter:title" content="truncate | share-open">
  <meta property="og:title" content="truncate | share-open">
  </head>
  <body>
    <div id="app"><div class="layout"><header class="layout-header-wrap clearfix"><div class="logo"><a href="/share-open/">minionorg share</a></div><ul class="menu-navs clearfix"><li class="nav-item"><a href="/share-open/guide/introduction">指南</a></li><li class="nav-item"><a href="/share-open/ArrayUtils/add">功能</a></li></ul></header><div class="layout-main"><div class="layout-siderbar"><div class="withoutScroll siderbar-wrap"><ul class="siderbar"><!--[--><!--]--></ul></div></div><div class="layout-container"><div class="content chestnut-doc"><div style="position:relative;"><div><h2 id="truncate" tabindex="-1">truncate <a class="header-anchor" href="#truncate" aria-hidden="true">#</a></h2><div class="language-js"><pre><code> <span class="token function">truncate</span><span class="token punctuation">(</span>str<span class="token operator">:</span> string<span class="token punctuation">,</span> <span class="token literal-property property">offset</span><span class="token operator">:</span> number<span class="token punctuation">,</span> <span class="token literal-property property">maxWidth</span><span class="token operator">:</span> number<span class="token punctuation">)</span><span class="token operator">:</span> string 
</code></pre></div><p>截断字符串</p><h3 id="参数" tabindex="-1">参数 <a class="header-anchor" href="#参数" aria-hidden="true">#</a></h3><ul><li><!----> str — 要截断的字符串,可能为null</li><li><!----> offset — 位置</li><li><!----> maxWidth — 结果的最大长度字符串,必须为正整数</li></ul><h3 id="返回值" tabindex="-1">返回值 <a class="header-anchor" href="#返回值" aria-hidden="true">#</a></h3><ul><li><!----> 截断字符串</li></ul><h3 id="案例" tabindex="-1">案例 <a class="header-anchor" href="#案例" aria-hidden="true">#</a></h3><div class="language-js"><pre><code><span class="token function">truncate</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghij&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;abc&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghij&quot;</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;fghij&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;raspberry peach&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;peach&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;abcdefghij&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RangeError</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MIN_SAFE_INTEGER</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RangeError</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MIN_SAFE_INTEGER</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RangeError</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;abcdefghijklmno&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;bcdefghijk&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;cdefghijkl&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;defghijklm&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;efghijklmn&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;fghijklmno&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;fghij&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;fgh&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;klm&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;klmno&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;n&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;no&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;o&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;o&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghijklmno&quot;</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">,</span> Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghij&quot;</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RangeError</span>
<span class="token function">truncate</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefghij&quot;</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RangeError</span>
</code></pre></div></div></div><div class="layout-antorns"><div class="antorns-wrap"><div class="antorn-container"><div class="antorn-line"><span style="top:8px;" class="antorn-line-i"></span></div><ul class="antorn-list"><!--[--><li class=""><a data-href="#truncate" href="#truncate">truncate</a></li><li class=""><a data-href="#参数" href="#参数">参数</a></li><li class=""><a data-href="#返回值" href="#返回值">返回值</a></li><li class=""><a data-href="#案例" href="#案例">案例</a></li><!--]--></ul></div></div></div></div></div></div><div class="footer" data-v-151d3a08><div class="container" data-v-151d3a08><div class="footer-main" data-v-151d3a08><h5 data-v-151d3a08>@minionorg Design</h5><!--[--><a href="http://minionorg.gitee.io/cli-open" class="footer-main-link" target="_blank" data-v-151d3a08><span data-v-151d3a08>@minionorg/cli</span><span class="sub" data-v-151d3a08> - </span><span class="sub" data-v-151d3a08>前端项目工程化工具</span></a><!--]--></div><div class="footer-main" data-v-151d3a08><h5 data-v-151d3a08>社区</h5><a href="http://minionorg.gitee.io/minionorg-issue-helper/?repo=minionorg/share-open" class="footer-main-link" target="_blank" data-v-151d3a08>问题收集</a></div><div class="footer-main" data-v-151d3a08><h5 data-v-151d3a08>友情链接</h5><a href="https://vue3js.cn/docs/zh/" class="footer-main-link" target="_blank" data-v-151d3a08>Vue</a><a href="https://cli.vuejs.org/" class="footer-main-link" target="_blank" data-v-151d3a08>Vue CLI</a></div></div><p class="for-the-record" data-v-151d3a08><img alt="NPM" src="https://img.shields.io/npm/l/@minionorg/share?color=%231890ff&amp;registry_uri=https%3A%2F%2Fregistry.npmjs.com&amp;style=flat-square" data-v-151d3a08><img alt="Copyright" src="https://img.shields.io/badge/©-Copyright 2021 @minionorg/share-1890ff?style=flat-square&amp;logo=copyright&amp;logoColor=white" data-v-151d3a08></p></div></div></div>
    <script>__VP_HASH_MAP__ = JSON.parse("{\"arrayutils_add.md\":\"6deecd3f\",\"arrayutils_addall.md\":\"55eece57\",\"arrayutils_clone.md\":\"4861e879\",\"arrayutils_remove.md\":\"ff78e67e\",\"arrayutils_toadded.md\":\"1c00a41a\",\"arrayutils_toremoved.md\":\"dfa2bc36\",\"charutils_compare.md\":\"a49b40e9\",\"charutils_isascii.md\":\"b8e8f522\",\"charutils_isasciialpha.md\":\"55f449d7\",\"charutils_isasciialphalower.md\":\"71cfade1\",\"charutils_isasciialphaupper.md\":\"3623a931\",\"charutils_isasciialphanumeric.md\":\"9e3163fa\",\"charutils_isasciicontrol.md\":\"beaf1b1b\",\"charutils_isasciinumeric.md\":\"ffaac4d7\",\"charutils_isasciiprintable.md\":\"21d03c6e\",\"charutils_iswhitespace.md\":\"44019c6e\",\"charutils_tochar.md\":\"a31cabfe\",\"charutils_tointvalue.md\":\"73fa4b55\",\"charutils_unicodeescaped.md\":\"23f56a51\",\"charutils_unicodeunescaped.md\":\"5e6f56fb\",\"dateutils_adddays.md\":\"e3fb0a93\",\"dateutils_addhours.md\":\"137c2a56\",\"dateutils_addminutes.md\":\"9fe32149\",\"dateutils_addmonths.md\":\"c1de5e6a\",\"dateutils_addsecs.md\":\"e0284dbe\",\"dateutils_addtime.md\":\"9ebf28e4\",\"dateutils_addyears.md\":\"cdb141ab\",\"dateutils_dateformathoc.md\":\"9665993b\",\"dateutils_getdateperiod.md\":\"010bc227\",\"dateutils_getdaysinmonth.md\":\"1058cb00\",\"dateutils_getfirstdateinmonth.md\":\"dedd0259\",\"dateutils_getlastdateinmonth.md\":\"fedca4f1\",\"dateutils_isdate.md\":\"19516b89\",\"dateutils_isleapyear.md\":\"7828d157\",\"dateutils_istime.md\":\"783f4aa6\",\"dateutils_timeformathoc.md\":\"76d49464\",\"dateutils_toisostring.md\":\"fdf9ef22\",\"numberutils_isfloat.md\":\"5b816b10\",\"numberutils_isinteger.md\":\"de99664c\",\"objectutils_allnotnull.md\":\"b9b03ff7\",\"objectutils_isempty.md\":\"57556d9a\",\"stringutils_abbreviate.md\":\"038ad8d5\",\"stringutils_contains.md\":\"37ff6cb2\",\"stringutils_equals.md\":\"5a240cbb\",\"stringutils_equalsany.md\":\"647c7ef8\",\"stringutils_equalsanyignorecase.md\":\"27419718\",\"stringutils_equalsignorecase.md\":\"88ee8bca\",\"stringutils_escapehtml.md\":\"7a0954e3\",\"stringutils_getstrlength.md\":\"f0cf809b\",\"stringutils_isanyempty.md\":\"2779df63\",\"stringutils_isen.md\":\"c17f4c14\",\"stringutils_isempty.md\":\"9c8a150e\",\"stringutils_iszh.md\":\"c489f0c0\",\"stringutils_length.md\":\"78b6a6ea\",\"stringutils_ltrim.md\":\"c9b1e5ff\",\"stringutils_rtrim.md\":\"2fc38725\",\"stringutils_startswith.md\":\"e16ef76e\",\"stringutils_strip.md\":\"94b449d1\",\"stringutils_stripall.md\":\"6c40f579\",\"stringutils_stripend.md\":\"0478dead\",\"stringutils_stripstart.md\":\"3f1932eb\",\"stringutils_striptoempty.md\":\"04549992\",\"stringutils_striptonull.md\":\"151b9b53\",\"stringutils_substring.md\":\"22a15930\",\"stringutils_substringafter.md\":\"a9d44462\",\"stringutils_substringafterlast.md\":\"8d51f18a\",\"stringutils_substringbefore.md\":\"fbffbaec\",\"stringutils_substringbeforelast.md\":\"96084028\",\"stringutils_substringbetween.md\":\"3e984b85\",\"stringutils_substringsbetween.md\":\"88661f94\",\"stringutils_trim.md\":\"1b170683\",\"stringutils_trimtoempty.md\":\"0f0974cd\",\"stringutils_trimtonull.md\":\"4c22a2d9\",\"stringutils_truncate.md\":\"debe512b\",\"stringutils_unescapehtml.md\":\"36f51783\",\"stringutils_uppercase.md\":\"ce31c73b\",\"stringutils_wrap.md\":\"7cb14ec4\",\"stringutils_wrapifmissing.md\":\"31ec6fa3\",\"utils_createtype.md\":\"c2aa6cab\",\"utils_geturlvaluebykey.md\":\"e32b9492\",\"utils_getvalue.md\":\"8ad16fff\",\"utils_hasemoji.md\":\"1214bfef\",\"utils_isemail.md\":\"2d35a693\",\"utils_isipv4.md\":\"b61c470f\",\"utils_isipv4range.md\":\"2a671d55\",\"utils_isipv6.md\":\"a04e5903\",\"utils_isipv6range.md\":\"26a82ee6\",\"utils_ismobile.md\":\"8586d0c6\",\"utils_isphone.md\":\"afa53472\",\"utils_isport.md\":\"2d70abf3\",\"utils_ispostalcode.md\":\"dfd1ee80\",\"utils_queryurlparams.md\":\"0cb29beb\",\"utils_validatetypehoc.md\":\"8749b24d\",\"guide_faq.md\":\"e345ea62\",\"guide_introduction.md\":\"98cf75ba\",\"guide_quickstart.md\":\"ab7970f9\",\"index.md\":\"2ba42b4c\"}")</script>
    <script type="module" async src="/share-open/assets/app.a7d6c65a.js"></script>
    
  </body>
</html>